package com.jlcloud.core.prometheus.config;

import com.alibaba.cloud.nacos.NacosDiscoveryProperties;
import com.jlcloud.core.prometheus.service.RegistrationService;
import com.jlcloud.core.launch.props.JlCloudPropertySource;
import com.jlcloud.core.prometheus.endpoint.AgentEndpoint;
import com.jlcloud.core.prometheus.endpoint.PrometheusApi;
import com.jlcloud.core.prometheus.endpoint.ReactivePrometheusApi;
import com.jlcloud.core.prometheus.endpoint.ServiceEndpoint;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cloud.client.ConditionalOnDiscoveryEnabled;
import org.springframework.cloud.client.ConditionalOnReactiveDiscoveryEnabled;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.discovery.ReactiveDiscoveryClient;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;

/**
 * Prometheus配置类
 *
 * @author By: JlCloud <br>
 * @Package: com.jlcloud <br>
 * @CreateTime: 2024-12-03 21:45 <br>
 * @Copyright: 2021 www.jilianjituan.com Inc. All rights reserved. <br>
 * @Caution 注意：本内容仅限于冀联人力集团内部传阅，禁止外泄以及用于其他的商业目的 <br>
 */
@AutoConfiguration
@JlCloudPropertySource(value = "classpath:/jlcloud-prometheus.yml")
public class PrometheusConfiguration {

	@Bean
	public RegistrationService registrationService(DiscoveryClient discoveryClient) {
		return new RegistrationService(discoveryClient);
	}

	@Bean
	public AgentEndpoint agentController(NacosDiscoveryProperties properties) {
		return new AgentEndpoint(properties);
	}

	@Bean
	public ServiceEndpoint serviceController(RegistrationService registrationService) {
		return new ServiceEndpoint(registrationService);
	}

	@AutoConfiguration
	@ConditionalOnBean(DiscoveryClient.class)
	@ConditionalOnDiscoveryEnabled
	// @ConditionalOnBlockingDiscoveryEnabled
	@ConditionalOnProperty(value = "spring.cloud.discovery.blocking.enabled")
	public static class PrometheusApiConfiguration {

		@Bean
		public PrometheusApi prometheusApi(Environment environment,
										   DiscoveryClient discoveryClient,
										   ApplicationEventPublisher eventPublisher) {
			String[] activeProfiles = environment.getActiveProfiles();
			String activeProfile = activeProfiles.length > 0 ? activeProfiles[0] : null;
			return new PrometheusApi(activeProfile, discoveryClient, eventPublisher);
		}

	}

	@AutoConfiguration
	@ConditionalOnBean(ReactiveDiscoveryClient.class)
	@ConditionalOnDiscoveryEnabled
	@ConditionalOnReactiveDiscoveryEnabled
	public static class ReactivePrometheusApiConfiguration {

		@Bean
		public ReactivePrometheusApi reactivePrometheusApi(Environment environment,
														   ReactiveDiscoveryClient discoveryClient,
														   ApplicationEventPublisher eventPublisher) {
			String[] activeProfiles = environment.getActiveProfiles();
			String activeProfile = activeProfiles.length > 0 ? activeProfiles[0] : null;
			return new ReactivePrometheusApi(activeProfile, discoveryClient, eventPublisher);
		}

	}

}
